package com.syh.jn.scm.service.impl.supplier.base.upstream;

import cn.redscarf.commons.wrapper.Restrictions;
import cn.redscarf.commons.wrapper.Wrapper;
import com.alibaba.dubbo.config.annotation.Service;
import com.syh.core.ddd.infrastructure.repository.AggregateRepository;
import com.syh.core.ddd.service.AbstractServiceImpl;
import com.syh.jn.scm.domain.supplier.entity.base.upstream.UpstreamCategory;
import com.syh.jn.scm.domain.supplier.repository.base.upstream.UpstreamCategoryRepository;
import com.syh.jn.scm.dto.supplier.base.upstream.cate.UpstreamCategoryMto;
import com.syh.jn.scm.dto.supplier.base.upstream.cate.UpstreamCategorySto;
import com.syh.jn.scm.dto.supplier.base.upstream.cate.UpstreamCategoryVo;
import com.syh.jn.scm.service.api.supplier.base.upstream.UpstreamCategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Transactional
public class UpstreamCategoryServiceImpl extends AbstractServiceImpl<UpstreamCategorySto, UpstreamCategoryMto, UpstreamCategoryVo, UpstreamCategory, Long> implements UpstreamCategoryService {

  @Autowired
  private UpstreamCategoryRepository upstreamCategoryRepositoryImpl;

  @Override
  protected AggregateRepository<UpstreamCategory, Long> getAggregateRepository() {
    return upstreamCategoryRepositoryImpl;
  }

  /**
   * 新增
   * @param sto
   * @return
   */
  @Override
  public Long save(UpstreamCategorySto sto) {
    UpstreamCategory category = new UpstreamCategory(sto);
    return upstreamCategoryRepositoryImpl.add(category);
  }

  /**
   * 修改
   * @param mto
   */
  @Override
  public void update(UpstreamCategoryMto mto) {
    UpstreamCategory category = upstreamCategoryRepositoryImpl.getById(mto.getId());
    category.modify(mto);
    upstreamCategoryRepositoryImpl.update(category);
  }

  /**
   * 删除
   * @param id
   */
  @Override
  public void delete(Long id) {
    UpstreamCategory category = upstreamCategoryRepositoryImpl.getById(id);
    category.destroy();
    upstreamCategoryRepositoryImpl.remove(category);
  }

  /**
   * 启用
   * @param id
   */
  @Override
  public void enabled(Long id) {
    this.recursionEnabled(id);
  }

  /**
   * 递归启用
   * @param id
   */
  private void recursionEnabled(Long id){
    UpstreamCategory category = upstreamCategoryRepositoryImpl.getById(id);
    //先启用父
    if (!Long.valueOf("0").equals(category.getSupId())) {
      UpstreamCategory categorySup = upstreamCategoryRepositoryImpl.getById(category.getSupId());
      categorySup.enabled();
      upstreamCategoryRepositoryImpl.update(categorySup);
      recursionEnabled(categorySup.getId());
    }
    //再启用子
    category.enabled();
    upstreamCategoryRepositoryImpl.update(category);
  }

  /**
   * 停用
   * @param id
   */
  @Override
  public void disabled(Long id) {
    this.recursionDisabled(id);
  }

  /**
   * 递归停用子类
   * @param id
   */
  private void recursionDisabled(Long id){
    UpstreamCategory category = upstreamCategoryRepositoryImpl.getById(id);
    //先把所有子停用
    Wrapper cateWrapper = Wrapper.build();
    cateWrapper.append(Restrictions.eq("supId", id));
    List<UpstreamCategory> upstreamCategories = upstreamCategoryRepositoryImpl.getList(cateWrapper);
    if (upstreamCategories != null && upstreamCategories.size() > 0) {
      upstreamCategories.stream().forEach((upstreamCategory)->{
        recursionDisabled(upstreamCategory.getId());
        upstreamCategory.disabled();
        upstreamCategoryRepositoryImpl.update(upstreamCategory);
      });
    }
    //再停用父的
    category.disabled();
    upstreamCategoryRepositoryImpl.update(category);
  }

}
